package cn.edu.service.impl;

import cn.edu.mapper.ExamineMapper;
import cn.edu.mapper.TeacherMapper;
import cn.edu.pojo.*;
import cn.edu.service.ExamineService;
import cn.edu.service.TeacherService;
import cn.edu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @author Davey chan
 */
public  class TeacherServiceImpl implements TeacherService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public PageBean<All> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
//     计算开始的索引
        int begin=(currentPage-1)*pageSize;
//        计算查询的条目数
        int size =pageSize;
//        查询当前页的数据
        List<All> rows = mapper.selectBypage(begin, size);
//        查询总记录数
        int totalCount = mapper.selectTotalCount();
//封装pageBean

        PageBean<All> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();
        return pageBean;
    }

    @Override
    public PageBean<All> selectByPageAndcondition(int currentPage, int pageSize,String academyName, All t) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
//     计算开始的索引
        int begin=(currentPage-1)*pageSize;
//        计算查询的条目数
        int size =pageSize;
//        处理查询条件，模糊查询
        String teacherName = t.getTeacherName();
        if(teacherName !=null && teacherName.length()>0){
            t.setTeacherName("%"+teacherName+"%");
        }
//        String status = t.getStatus();
//        if(status !=null && status.length()>0){
//            t.setStatus("%"+status+"%");
//
//        }
        t.setExamstatus("3");

        academyName="%"+academyName+"%";
        t.setAcademyName(academyName);


        List<All> rows = mapper.selectBypageAndCondtion(begin, size,t);
//        查询总记录数
        int totalCount = mapper.selectTotalCountByCondition(t);
//封装pageBean

        PageBean<All> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        sqlSession.close();
        return pageBean;
    }

    @Override
    public void delteacher(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
//     计算开始的索引
        mapper.delteacher(id);
        sqlSession.commit();
        sqlSession.close();
    }
    @Override
    public void Updateteacher(All t) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
//     计算开始的索引
        mapper.UpdateTeacher(t);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<thesis> getThesis(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
//        搜索论文的信息
        List<thesis> thesis = mapper.getThesis(id);
        return thesis ;
    }

    @Override
    public List<research_project> getResearch_project(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<research_project> research_project = mapper.getResearch_project(id);

        return research_project;
    }

    @Override
    public List<annual_examination> getAnnual_examination(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<annual_examination> annual_examination = mapper.getAnnual_examination(id);

        return annual_examination;
    }

    @Override
    public List<award> getAward(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<award> award = mapper.getAward(id);

        return award;
    }

    @Override
    public List<honor_title> getHonor_title(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<honor_title> honor_title = mapper.getHonor_title(id);

        return honor_title;

    }

    @Override
    public List<opus> getOpus(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<opus> opus = mapper.getOpus(id);

        return opus;
    }

    @Override
    public List<teach_work> getTeach_work(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<teach_work> teach_work = mapper.getTeach_work(id);
        return teach_work;
    }

    @Override
    public List<train> getTrain(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<train> train = mapper.getTrain(id);
        return train;

    }

    @Override
    public List<patent> getPatent(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<patent> patent = mapper.getPatent(id);
        return patent;
    }

    @Override
    public List<work> getWork(Integer id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//      获取mapper对象
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<work> work = mapper.getWork(id);
        return work;
    }
}
